﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class page_ShopStock_Manager : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Utility.InitUltraWebGrid.InitDisplayLayOut(this.uwgMain);

            if (_Site != null)
            {
                tSiteID.Text = _Site.ID.ToString();
            }

            BindData();
            ReadAction();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        base.OnLoad(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        string Condtion = "ShopID=" + _Site.ID + " and " + ddlType.SelectedValue + " like '%" + txtCondition.Text + "%'";
        dt = new DAL.Views.V_Shop_ShopStock().Open("", Shove._Web.Utility.FilteSqlInfusion(Condtion), "");

        this.uwgMain.DataSource = dt;
        this.uwgMain.DataBind();
    }

    protected void uwgMain_UpdateRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e)
    {
        string Type = Shove._Web.Utility.GetRequest("Type");
        
        DAL.Tables.tbl_Shop_ShopStock stock = new DAL.Tables.tbl_Shop_ShopStock();

        long ID = Shove._Convert.StrToLong(e.Row.Cells[0].Text,0);
        int UnMarketedNum = 0;
        DateTime UnMarketedDate = DateTime.Now;

        if (Type == "AlertAmount")
        {
            try
            {
                UnMarketedNum = Convert.ToInt32(e.Row.Cells[9].Text);
            }
            catch
            {
                Shove._Web.JavaScript.Alert(this.Page, "请输入数字！");

                return;
            }

            stock.UnMarketedNum.Value = UnMarketedNum;
        }

        if (Type == "AlertDate")
        {
            try
            {
                UnMarketedDate = Convert.ToDateTime(e.Row.Cells[10].Text);
            }
            catch
            {
                Shove._Web.JavaScript.Alert(this.Page, "日期输入错误！");

                return;
            }

            stock.UnMarketedDate.Value = UnMarketedDate;
        }

        long flag = stock.Update("ID="+ID.ToString());

        if (flag >= 0)   
        {
            Shove._Web.JavaScript.Alert(this.Page, "设置成功");
            BindData();
        }
        else
        {
            Shove._Web.JavaScript.Alert(this.Page, "设置失败");
        }
    }                  

    protected void uwgMain_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        this.BindData();     
    }

    private void ReadAction()
    {
        string Type = Shove._Web.Utility.GetRequest("Type");

        if (Type == "AlertAmount")
        {            
            uwgMain.Columns[10].Hidden = true;
            SetBackColorByAlertType(Type);
        }
        else if (Type == "AlertDate")
        {
            WebPanel1.Header.Text = "滞销预警设置";
            uwgMain.Columns[9].Hidden = true;
            SetBackColorByAlertType(Type);
        }
        else if (Type == "StockQuery")
        {
            WebPanel1.Header.Text = "商品库存";
            uwgMain.Columns[9].Hidden = true;
            uwgMain.Columns[10].Hidden = true;
        }
    }

    private void SetBackColorByAlertType(string Type)
    {
        foreach (Infragistics.WebUI.UltraWebGrid.UltraGridRow r in uwgMain.Rows)
        {
            bool  AlertAmountFlag = false;
            bool  AlertDateFlag = false;

            int StockAmount = Shove._Convert.StrToInt(r.Cells[8].Text,0);
            int AlertAmount = Shove._Convert.StrToInt(r.Cells[9].Text,0);
            DateTime dtAlertDate = Shove._Convert.StrToDateTime(r.Cells[10].Text,DateTime.Now.ToString("yyyy-MM-dd"));
            TimeSpan ts = dtAlertDate.Subtract(DateTime.Now);

            if((StockAmount - AlertAmount) <= 0 )
            {
                AlertAmountFlag = true;
            }

            if (ts.Days < 0)
            {
                AlertDateFlag = true;
            }

            if(Type == "AlertAmount")
            {
                if (AlertAmountFlag)
                {
                    r.Style.BackColor = System.Drawing.Color.Red;
                }
            }
            else if (Type == "AlertDate")
            {
                if (AlertDateFlag)
                {
                    r.Style.BackColor = System.Drawing.Color.Red;
                }
            }
        }
    }

    private void BindData()
    {
        long SiteID = Shove._Convert.StrToLong(tSiteID.Text, 0);

        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        string FieldList = "ID,ShopNo,GoodNo,GoodName_CN,GoodNorm,GoodColor,Price_Orig,Price_Vip,Amount,UnMarketedNum,UnMarketedDate";

        dt = new DAL.Views.V_Shop_ShopStock().Open(FieldList, "ShopID=" + SiteID, "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无您需要的数据", this.GetType().BaseType.FullName);

            return;
        }

        this.uwgMain.DataSource = dt;
        this.uwgMain.DataBind();

        string Type = Shove._Web.Utility.GetRequest("Type");

        if (Type == "AlertAmount")
        {
            uwgMain.Columns[10].Hidden = true;
            SetBackColorByAlertType(Type);
        }
        else if (Type == "AlertDate")
        {
            uwgMain.Columns[9].Hidden = true;
            SetBackColorByAlertType(Type);
        }
        else if (Type == "StockQuery")
        {

        }
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Remove("UnMarketedNum");
            dt.Columns.Remove("UnMarketedDate");

            dt.Columns[0].ColumnName = "序号";
            dt.Columns[1].ColumnName = "店铺编号";
            dt.Columns[2].ColumnName = "商品编号";
            dt.Columns[3].ColumnName = "商品名称";
            dt.Columns[4].ColumnName = "商品规格";
            dt.Columns[5].ColumnName = "商品颜色";
            dt.Columns[6].ColumnName = "商品价格";
            dt.Columns[7].ColumnName = "会员价格";
            dt.Columns[8].ColumnName = "库存数量";  
 
            Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "ShopStock.xls");
        }
        else
        {
            PublicFunction.GoError(ErrorNumber.NoData, "没有数据，不能导出到Excel!", "ShopStock");
        }
    }
}
